﻿'Imports userModel
Imports System
Imports ADOEntity

Partial Class AML_changePassword2
    Inherits System.Web.UI.Page

    Dim sUserEntity As UserEntities
    Dim clsEdmUser As New clsEdmUser
    Dim clsMenuRight As New clsSessionMenuRight
    Dim strMsg As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim UserEntity As New UserEntities
        clsMenuRight = Session("ClsMenuRight")

        If Not IsPostBack Then
            'chkIsLogin()
            txtUsername.Text = Request.QueryString("user")
            If (txtUsername.Text = "") Then
                txtUsername.Enabled = True
            Else
                txtUsername.Enabled = False
            End If

            'If clsMenuRight.getIsPwdExpire = 1 Or clsMenuRight.getMustChangePwd = 1 Then
            '    If clsMenuRight.getRoleGroup < 70 Then
            '        If clsMenuRight.getRoleGroup < 70 And clsMenuRight.getRoleGroup > 59 Then
            '            'Dim qryUser = From tblUser In UserEntity.SUSER
            '            'Where tblUser.

            '        ElseIf clsMenuRight.getRoleGroup < 60 And clsMenuRight.getRoleGroup > 49 Then

            '        ElseIf clsMenuRight.getRoleGroup < 49 Then
            '            Dim qryUser = From tblUser In UserEntity.SUSER

            '        End If
            '    End If

            'End If
            CreateControlMP()
        End If
    End Sub

    Sub CreateControlMP()
        '  "เปลี่ยนรหัสผ่าน"
        lblPage.Text = "เปลี่ยนรหัสผ่าน"
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        sUserEntity = New UserEntities
        Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUsername.Text).ToList

        Dim strIpLogin As String = ""
        Dim strComName As String = ""
        strIpLogin = Request.UserHostAddress
        strComName = Request.UserHostName


        Dim clsMenuRight As New clsSessionMenuRight
        clsMenuRight = clsEdmUser.checkUser(txtUsername.Text.Trim, _
                                            txtOldPwd.Text.Trim, strIpLogin, strComName)


        If (qry Is Nothing Or qry.Count = 0) Then
            strMsg = "ชื่อผู้ใช้งานไม่ถูกต้อง หรือ สถานะผู้ใช้งานไม่ได้ถูกเปิดใช้"

        ElseIf (chkpassword()) Then
            If Not qry(0).USERNAME = "" Then
                If (clsEdmUser.saveChangePassword(qry(0).USERNAME, txtOldPwd.Text, txtNewPwd.Text)) Then
                    'clsMenuRight.getIsPwdExpire = 0
                    'clsMenuRight.getMustChangePwd = 0

                    'Login ให้อัตโนมัติ แล้วส่งไปหน้า News.aspx
                    Login(txtUsername.Text, txtNewPwd.Text)
                    'Session("ClsMenuRight") = clsMenuRight
                    'Response.Redirect("News.aspx")
                Else
                    If Not (clsEdmUser.ErrExp = "") Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", String.Format("alert('{0}')", clsEdmUser.ErrExp), True)
                    Else
                        If Not (clsEdmUser.Msg = "") Then
                            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", String.Format("alert('{0}')", clsEdmUser.Msg), True)

                        End If
                    End If
                End If
            Else

            End If
        Else
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", String.Format("alert('{0}')", strMsg), True)
        End If

        clsMenuRight = Nothing
    End Sub

    Private Function chkpassword() As Boolean
        Dim chkstatus As Boolean = True
        Dim clsCheckPwd As clsCheckPwd = New clsCheckPwd
        Dim clsSystemConfig As clsSystemConfig = New clsSystemConfig

        If (txtOldPwd.Text.Trim = "") Then
            strMsg = String.Format("{0}{1}\n", strMsg, "กรุณาระบุรหัสผ่าน")
            Return chkstatus = False
        End If

        If (txtNewPwd.Text.Trim = "" Or txtRePwd.Text.Trim = "") Then
            strMsg = String.Format("{0}{1}\n", strMsg, String.Format(Resources.resValidate.validate_TextLength, Resources.resUser.password))
            Return chkstatus = False
        Else
            ' lblChkBetweenPwd.Visible = False
        End If

        If (txtNewPwd.Text.Length > 7) Then
            If Not (txtNewPwd.Text.Trim = txtRePwd.Text.Trim) Then
                strMsg = String.Format("{0}{1}\n", strMsg, Resources.resValidate.validate_TextPassword)
                Return chkstatus = False
            Else
                '   lblChkPassword.Visible = False
            End If
        Else
            strMsg = String.Format("{0}{1}\n", strMsg, String.Format(Resources.resValidate.validate_TextLength, Resources.resUser.password))
            Return chkstatus = False
        End If

        If Not (clsCheckPwd.checkpassword(txtNewPwd.Text)) Then
            strMsg = String.Format("{0}{1}\n", strMsg, Resources.resValidateUser.NotPasswordStrong)
            chkstatus = False
        End If

        clsSystemConfig.setdefaultValueString()
        If (clsSystemConfig.chkDupPwd(txtNewPwd.Text)) Then
            strMsg = String.Format("{0}{1}\n", strMsg, Resources.resValidateUser.PasswordDuplicate)
            chkstatus = False
        End If

        clsSystemConfig = Nothing


        Return chkstatus
    End Function

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncancel.Click

        Session.Abandon()
        Response.Redirect("index.aspx")

        'If (clsMenuRight.getIsPwdExpire = "" Or clsMenuRight.getIsPwdExpire = "0") And (clsMenuRight.getMustChangePwd = "" Or clsMenuRight.getMustChangePwd = "0") Then
        '    Response.Redirect("News.aspx")
        'Else
        '    Session.Abandon()
        '    Response.Redirect("index.aspx")

        'End If
    End Sub

    Private Sub Login(ByVal userName As String, ByVal password As String)
        Dim strIpLogin As String = ""
        Dim strComName As String = ""
        strIpLogin = Request.UserHostAddress
        strComName = Request.UserHostName


        Dim clsMenuRight As New clsSessionMenuRight
        clsMenuRight = clsEdmUser.checkUser(userName.Trim, _
                                            password.Trim, strIpLogin, strComName)

        If Not clsMenuRight.getUserName = "" Then
            lblErrorMsg.Visible = False
            Session("ClsMenuRight") = clsMenuRight

            If (clsEdmUser.chkMustChangePwd = True) Then
                If Not (clsEdmUser.Msg = "") Then
                    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                    String.Format("alert('{0}');location.href = 'changePassword.aspx';", clsEdmUser.Msg), True)
                Else
                    Response.Redirect("changePassword.aspx")
                End If


            Else

                If (clsEdmUser.Msg = "") Then
                    'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                    '                                "alert('" + clsEdmUser.Msg.Replace("'", "") + "');location.href = 'News.aspx';", True)
                    Response.Redirect("News.aspx")
                Else
                    If (clsMenuRight.getIsPwdExpire = 1) Then
                        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                  String.Format("alert('{0}');location.href = 'changePassword.aspx';", clsEdmUser.Msg.Replace("'", "")), True)
                    Else
                        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                                                    String.Format("alert('{0}');location.href = 'News.aspx';", clsEdmUser.Msg.Replace("'", "")), True)
                        ' Response.Redirect("News.aspx")
                    End If


                End If

            End If

        Else
            If Not (clsEdmUser.ErrExp = "") Then
                'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                '                                   "alert('" + clsEdmUser.ErrExp.Replace("'", "") + "');", True)
                ScriptManager.RegisterStartupScript(Me, _
                                                                  Me.GetType(), _
                                                                 "clientScript", _
                                                                 String.Format("alert('{0}');", clsEdmUser.ErrExp.Replace("'", "")), _
                                                                 True)
            Else
                If Not (clsEdmUser.Msg = "") Then

                    'If clsEdmUser.Msg = "isPwdExp" Then
                    '    'ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                    '    '"alert('รหัสผ่านหมดอายุ กรุณาเปลี่ยนรหัสผ่าน');location.href = 'changePassword.aspx?PwdExp=1';", True)
                    '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "message", _
                    '                              "alert('" + clsEdmUser.Msg + "');location.href = 'changePassword.aspx';", True)
                    'Else
                    ScriptManager.RegisterStartupScript(Me, _
                                                                                      Me.GetType(), _
                                                                                     "clientScript", _
                                                                                     String.Format("alert('{0}');", clsEdmUser.Msg.Replace("'", "")), _
                                                                                     True)
                    'End If


                End If

            End If

            'lblErrorMsg.Visible = True
            'txtPassword.Text = ""
        End If

    End Sub
End Class
